﻿using System;
using System.Collections.Generic;
using System.IO;
using E4RPTools;

namespace DBTractores
{
    public class PartReader : IProgressWorker
    {
        #region Members

        private readonly Dictionary<string, Part> _parts;
        private readonly Result _errors;

        #endregion

        #region Events

        public event EventHandler DocumentProcessing;

        #endregion

        #region Properties

        public string FilePath { get; set; }

        public int DocumentCount { get; private set; }

        public int DocumentIndex { get; private set; }

        public string DocumentNumber { get; private set; }

        #endregion

        #region Public Methods

        public PartReader(Result errors, Dictionary<string, Part> parts)
        {
            _parts = parts;
            _errors = errors;
        }

        public bool Work()
        {
            _errors.Reset();
            _parts.Clear();
            StreamReader file = null;
            try
            {
                file = new StreamReader(FilePath);
                DocumentCount = Convert.ToInt32(file.BaseStream.Length / 117);
                DocumentIndex = 0;
                return ReadParts(file);
            }
            catch (Exception exception)
            {
                if (file != null)
                    file.Close();
                _errors.Add(DocumentIndex.ToString(), "Ficheiro corrompido. " + exception.Message);
                return false;
            }
        }

        private bool ReadParts(TextReader file)
        {
            while (true)
            {
                var line = file.ReadLine();
                if (string.IsNullOrEmpty(line))
                    return true;
                var part = Add(line);
                DocumentNumber = part.Code;
                DocumentIndex++;
                OnDocumentProcessing();
            }
        }

        #endregion

        #region Protected Methods

        virtual protected void OnDocumentProcessing()
        {
            var documentProcessing = DocumentProcessing;
            if (documentProcessing != null)
                documentProcessing(this, new EventArgs());
        }

        #endregion

        #region Private Methods

        private Part Add(string line)
        {
            var part = new Part
            {
                Code = line.Substring(0, 18).TrimEnd(' '),
                Description = line.Substring(18, 40),
                Flag = line.Substring(58, 2),
                Date = new DateTime(Convert.ToInt32(line.Substring(60, 4)), Convert.ToInt32(line.Substring(64, 2)), Convert.ToInt32(line.Substring(66, 2))),
                Price = (Convert.ToDouble(line.Substring(68, 11))/100.0),
                Wheight = (Convert.ToDouble(line.Substring(79, 13))/1000.0),
                Quantity = Convert.ToInt32(line.Substring(92, 5)),
                Line = line.Substring(97, 4),
                SDC = line.Substring(101, 1),
                PCC = line.Substring(102, 5),
                MPC = line.Substring(107, 5),
                Material = line.Substring(112, 1)
            };
            _parts.Add(part.Code, part);
            return part;
        }

        #endregion
    }
}
